package com.cho3;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.cho3.dialog.UpdateDialog;
import com.cho3.entity.UpdateInfo;
import com.cho3.net.APIFunction;
import com.cho3.net.RetrofitFactory;
import com.cho3.net.RxSchedulers;
import com.cho3.net.base.BaseObserver;
import com.cho3.net.bean.BaseEntity;

import java.io.File;

import io.reactivex.Observable;

public class SplashActivity extends AppCompatActivity {
    private static final String TAG = "SplashActivity";
    private String bundleVersion = BuildConfig.BUNDLE_VERSION;
    private String bundleParentPath = null;
    private String bundlePath = null;
    private String bundleName = "index.android.bundle";
    private File bundleFile = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        TextView tvWelcome = findViewById(R.id.welcome);
        tvWelcome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
        });
        checkJsBundleVersion();
    }

    private void checkJsBundleVersion() {
        String js_bundle_version = CacheDiskUtils.getInstance().getString("js_bundle_version");
        if (js_bundle_version != null) {
            bundleVersion = js_bundle_version;
        }
        Observable<BaseEntity<UpdateInfo>> observable = RetrofitFactory.getInstence().createService(APIFunction.class).checkJsBundleUpdate(bundleVersion, "2");

        observable.compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<UpdateInfo>() {
                    @Override
                    protected void onSucceed(UpdateInfo updateInfo) {
                        if (!TextUtils.isEmpty(updateInfo.getChangeLog())) {
                            Log.i(TAG, updateInfo.getDownloadUrl());
                            new UpdateDialog(SplashActivity.this, updateInfo.getDownloadUrl(),updateInfo.getVersion()) {

                                @Override
                                public void jumpToMain(boolean isGoTo) {
                                    if (isGoTo) {
                                        startActivity(new Intent(SplashActivity.this, MainActivity.class), ActivityOptions.makeSceneTransitionAnimation(SplashActivity.this).toBundle());
                                    }
                                }
                            }.show();
                        } else {
                            startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        }
                    }

                    @Override
                    protected void onCodeError(String error_code, String reason) {
                        super.onCodeError(error_code, reason);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        Toast.makeText(SplashActivity.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
